package hu.uniobuda.nik.openday.rss;

import java.util.List;

import hu.uniobuda.nik.openday.CustomLinkMovementMethod;
import hu.uniobuda.nik.openday.R;
import hu.uniobuda.nik.openday.utils.Config;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class RssArrayAdapter extends ArrayAdapter<RssItem> {

	private Typeface font;

	public RssArrayAdapter(Context context, List<RssItem> objects) {
		super(context, R.layout.rss_template, objects);
		font = Typeface.createFromAsset(context.getAssets(), Config.FONT);
	}

	// @Override
	// public boolean isEnabled(int position) { return false; }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		View row = convertView;
		if (row == null) {
			row = View
					.inflate(parent.getContext(), R.layout.rss_template, null);
			holder = new ViewHolder();
			holder.date = (TextView) row.findViewById(R.id.txtDate);
			holder.title = (TextView) row.findViewById(R.id.txtTitle);
			holder.desc = (TextView) row.findViewById(R.id.txtDescription);
			row.setTag(holder);

		} else {
			holder = (ViewHolder) row.getTag();
		}

		RssItem item = getItem(position);

		holder.date.setTypeface(font);
		holder.date.setText(item.getFriendlyDate());
		holder.title.setTypeface(font, Typeface.BOLD);
		holder.title.setText(item.getTitle());
		holder.desc.setTypeface(font);
		holder.desc.setText(Html.fromHtml(item.getDescription()),TextView.BufferType.SPANNABLE);
		holder.desc.setMaxLines(Config.MAX_LINES);
		holder.desc.setTag(true);	
		holder.desc.setMovementMethod(CustomLinkMovementMethod.getInstance());
		return row;
	}		

	static class ViewHolder {
		TextView date, title, desc;
	}
}
